package com.ts.tube.utils;

public class ConvertUtils {
    /**
     * Function to change progress to timer
     * 
     * @param progress
     *            : progress percentage in int
     * @param totalDuration
     *            : total duration of song in milliseconds
     * @return current duration in milliseconds
     * */
    public static int progressToTimer(final int progress, final int totalDuration) {
        int currentDuration = 0;
        int duration = (int) (totalDuration / 1000);
        currentDuration = (int) ((((double) progress) / 100) * duration);
        return currentDuration * 1000;
    }

    /**
     * milliSecondsToTimer(int milliseconds) converts milliseconds to time in string format
     * 
     * @param milliseconds
     * @return time string
     */
    public static String milliSecondsToTimer(final int milliseconds) {
        String finalTimerString = "";
        String secondsString = "";
        String strMins = "";
        // Convert total duration into time
        int hours = (int) (milliseconds / (1000 * 60 * 60));
        int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
        int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
        // Add hours if there
        if (hours > 0) {
            finalTimerString = hours + ":";
        }
        // Prepending 0 to seconds if it is one digit
        if (seconds < 10) {
            secondsString = "0" + seconds;
        } else {
            secondsString = "" + seconds;
        }
        // Prepending 0 to mins if it is one digit
        if (minutes < 10) {
            strMins = "0" + minutes;
        } else {
            strMins = "" + minutes;
        }
        finalTimerString = finalTimerString + strMins + ":" + secondsString;

        return finalTimerString;
    }
}
